use std::{collections::HashMap, sync::Arc};

use QcCore::ecs::{
    component::{Component, ComponentTrait},
    components::mesh_render::MeshRender,
};
use QcScript::core::JsComponent;
use QcUI::core::context::UiContext;

use crate::core::{context::Context, message::ComponentType};

pub mod component;
mod inspector_context;
pub mod ui;
pub use inspector_context::*;

use self::{
    component::{material_render::MaterialRenderState, mesh_render::MeshRenderState},
    ui::button::ButtonState,
};

pub trait InspectorTrait {
    fn inspector(&mut self, ctx: &mut InspectorContext, state: &mut ComponentState) {}
}

/// 用于属性面板存储ui界面的状态,每个组件都有一个
#[derive(Debug)]
pub enum ComponentState {
    Camera,
    Light,
    MaterialRender(MaterialRenderState),
    MeshRender(MeshRenderState),
    SkyBox,
    Transform,
    JsComponent,
    Other,
    Button(ButtonState),
}

#[derive(Debug)]
pub struct ComponentStateList {
    map: HashMap<ComponentType, ComponentState>,
}

impl ComponentStateList {
    pub fn new(context: Arc<Context>) -> Self {
        let mut hashmap = HashMap::new();
        hashmap.insert(ComponentType::Camera, ComponentState::Camera);
        hashmap.insert(ComponentType::Light, ComponentState::Light);
        hashmap.insert(
            ComponentType::MeshRender,
            ComponentState::MeshRender(MeshRenderState::new(context.clone())),
        );
        hashmap.insert(ComponentType::SkyBox, ComponentState::SkyBox);
        hashmap.insert(ComponentType::Transform, ComponentState::Transform);
        hashmap.insert(ComponentType::Other, ComponentState::Other);
        hashmap.insert(
            ComponentType::MaterialRender,
            ComponentState::MaterialRender(MaterialRenderState::new(context.clone())),
        );
        hashmap.insert(ComponentType::Other, ComponentState::Other);

        hashmap.insert(ComponentType::Widget, ComponentState::Other);

        hashmap.insert(
            ComponentType::Button,
            ComponentState::Button(ButtonState::new(context.clone())),
        );
        hashmap.insert(ComponentType::Laebl, ComponentState::Other);
        hashmap.insert(ComponentType::TextBox, ComponentState::Other);
        hashmap.insert(ComponentType::Grid, ComponentState::Other);
        hashmap.insert(ComponentType::Panel, ComponentState::Other);

        ComponentStateList { map: hashmap }
    }

    pub fn get_state(&self, comp: ComponentType) -> &ComponentState {
        self.map.get(&comp).unwrap()
    }

    pub fn get_state_mut(&mut self, comp: ComponentType) -> &mut ComponentState {
        self.map.get_mut(&comp).unwrap()
    }

    pub fn refresh(&mut self){
        
    }
}

pub struct InspectorMap;
